<?php

/**
 *
 * @author ben <ben@spicatech.cn>  Created on 2015-7-5
 */
class PayAccount extends MainActiveRecord {

    const ACCOUNT_TYPE_ALIPAY = 0;
    const ACCOUNT_TYPE_WECHATPAY = 1;

    public function tableName() {
        return 'micro_pay_account';
    }

    /**
     * 
     * @param type $className
     * @return PayAccount
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 
     * @param type $clientId
     * @param type $accountType
     * @return PayAccount
     */
    public static function findAccount($clientId, $accountType) {
        $c = new CDbCriteria();
        $c->addColumnCondition(array(
            'wid'=>$clientId,
            'account_type'=>$accountType,
            'active'=>1
        ));
        $c->order = 'order_index asc';
        return self::model()->find($c);
    }
    
    /**
     * 
     * @param type $clientId
     * @return array {"app_id":"","secret_key":"","pay_key":"","mchid":""}
     */
    public static function getWechatPayParams($clientId) {
        $model = self::findAccount($clientId, self::ACCOUNT_TYPE_WECHATPAY);
        if(!$model) {
            return array();
        }
        $params = json_decode($model->api_params, true);
        
        return $params;
    }

}
